﻿
@{
    ViewData["Title"] = "ProcessUpdate";
    Layout = "~/Views/Home/Menu.cshtml";
}

<h1>工序修改</h1>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序编号</label>
            <div class="layui-input-inline">
                <input type="text" name="processNumebring" id="processNumebring" placeholder="请输入" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">工序名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="processName" id="processName" placeholder="请输入" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="processStatus" id="processStatus" value="启用" title="启用" checked>
            <input type="radio" name="processStatus" id="processStatus" value="禁用" title="禁用">
        </div>
    </div>


    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">工序说明</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="processIllustrate" id="processIllustrate" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="processReamrk" id="processReamrk" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建日期</label>
        <div class="layui-input-inline layui-input-wrap">
            <input type="date" name="processDate" id="processDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" style="float:left">
            <button type="submit" class="layui-btn" lay-submit lay-filter="ProcessSbumit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="location.href='/Process/ProcessHow'" >返回</button>
        </div>
    </div>
</form>

<script>
    $(function () { 
        fantian()
    })
    var obj = {
        processId: 0,
        processNumebring: "",
        processName: "",
        processStatus: "",
        processIllustrate: "",
        processReamrk: "",
        processDate: "",
        addName: "张三",
        addTime: "",
    }
    function fantian() {
        return new Promise(function (resolve, reject) {
            $(document).ready(function () {
                // 获取要编辑的用户ID，可以通过URL参数或者其他方式获取，这里假设通过URL参数获取
                var processId = localStorage.getItem("processId");
                console.log(processId);
                // 如果获取到了用户ID，就去后台获取该用户的详细信息并反填到表单
                if (processId) {
                    debugger
                    $.ajax({
                        type: "get",
                        url: "/Process/ProcessModel",
                        data: {
                            id: processId
                        },
                        success: function (res) {
                            var resse = JSON.parse(res)
                            console.log(resse)
                            if (resse.data) {
                                const s = resse.data;
                                //反填日期框
                                var da = new Date(s.processDate);
                                // 获取年、月、日
                                var year = da.getFullYear();
                                var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                                var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
                                // 拼接成 "yyyy-MM-dd" 格式
                                var formattedDate = `${year}-${month}-${day}`;
                                $("#processId").val(s.processId)
                                $("#processNumebring").val(s.processNumebring)
                                $("#processName").val(s.processName)
                                $("#processStatus").val(s.processStatus)
                                var lswarehouseState = s.processStatus
                                $("[name='processStatus']").each(function () {
                                    if (lswarehouseState.toString() == $(this).val()) {
                                        $(this).prop("checked", true);
                                    }
                                })
                                $("#processIllustrate").val(s.processIllustrate)
                                $("#processReamrk").val(s.processReamrk)
                                $("#processDate").val(formattedDate)
                                $("#processNumebring").val(s.processNumebring)
                                $("#addName").val(s.addName)
                                $("#addTime").val(s.addTime)
                                obj.processId = s.processId,
                                    obj.processNumebring = s.processNumebring,
                                    obj.processName = s.processName,
                                    obj.processStatus = s.processStatus,
                                    obj.processIllustrate = s.processIllustrate,
                                    obj.processReamrk = s.processReamrk,
                                    obj.processDate = s.processDate,
                                    obj.addName = s.addName,
                                    obj.addTime = s.addTime
                            }
                        }
                    })
                }

            });
        });
    };

    var obj1 = {
        processId: 0,
        processNumebring: "",
        processName: "",
        processStatus: "",
        processIllustrate: "",
        processReamrk: "",
        processDate: "",
        addName: "张三",
        addTime: "",
    }
    layui.use(['layer', 'form', 'util'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var util = layui.util;

        form.on('submit(ProcessSbumit)', function (data) {
            var field = data.field; // 获取表单字段值
            console.log(field)
            obj1.processId = obj.processId,
                obj1.processNumebring = field.processNumebring,
                obj1.processName = field.processName,
                obj1.processStatus = $('#processStatus').is(':checked') ?"启用": "禁用",
                obj1.processIllustrate = field.processIllustrate,
                obj1.processReamrk = field.processReamrk,
                obj1.processDate = field.processDate,
                obj1.addTime = obj.addTime,
                obj1.addName = obj.addName,
                console.log(obj1)
            // 显示填写结果，仅作演示用
            // 此处可执行 Ajax 等操作
            debugger
            $.ajax({
                url: '/Process/ProcessUpdatese',
                type: 'post',
                data: { dto: obj1, token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    console.log(obj1);
                    console.log(res);
                    if (res.data == 1) {
                        layer.msg("修改成功", { icon: 1 })
                        setTimeout(function () {
                            location.href = '/Process/ProcessHow'
                        }, 2000);
                    } else {
                        layer.msg("修改失败:" + res.message, { icon: 2 });
                    }
                }
            });

            return false; // 阻止默认 form 跳转
        });
        // 对弹层中的表单进行初始化渲染
        form.render();
    });
    
</script>